package top.zhangjianyong.tools.factory;

import lombok.extern.slf4j.Slf4j;
import top.zhangjianyong.tools.entity.WxBill;
import top.zhangjianyong.tools.utils.DateTimeUtil;
import top.zhangjianyong.tools.utils.StringUtil;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
public class WxBillFactory {
    public static WxBill create() {
        return new WxBill();
    }

    public static WxBill create(String line, Long fileId) {
        log.info("微信账单：" + line);
        WxBill wxBill = create();
        List<String> split = StringUtil.regexSplit(line, ",");
        if (split.size() < 11) throw new RuntimeException("分割对账数据异常");
        wxBill.setTradeTime(DateTimeUtil.localDateTime(split.get(0), "yyyy-MM-dd HH:mm:ss"));
        wxBill.setType(split.get(1));
        wxBill.setCounterparty(split.get(2));
        wxBill.setCommodity(StringUtil.trimFirstAndLastChar(split.get(3), "\""));
        wxBill.setInOrOut(split.get(4));
        wxBill.setAmt(new BigDecimal(split.get(5).replaceAll("¥", "")));
        wxBill.setPayWay(split.get(6));
        wxBill.setState(split.get(7));
        wxBill.setOrderNo(split.get(8));
        wxBill.setMerchantOrderNo(split.get(9));
        wxBill.setRemark(StringUtil.trimFirstAndLastChar(split.get(10), "\""));
        wxBill.setConvertState("初始");
        wxBill.setFileId(fileId);
        return wxBill;
    }
}
